﻿using ImperialServer.Common.Entities;
using ImperialUtilities.Exceptions;
using ImperialUtilities.Utilities;
using ImperialWebSite.Content.Resources;
using ImperialWebSite.Controllers;
using ImperialWebSite.Presenters.Status.Interfaces;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ImperialWebSite.Presenters.Status.Classes
{
    public class StatusRegistryPresenter
    {
        private IStatusRegistry _view;
        private StatusController _controller;
        private EventViewer _logger;

        /// <summary>
        /// Contructor de la clase
        /// </summary>
        /// <param name="view">Interfaz que enlaza con la vista</param>
        public StatusRegistryPresenter(IStatusRegistry view)
        {
            _view = view;
            _controller = new StatusController();
            _logger = new EventViewer("ImperialWebSite.Presenters.Status.Classes");
        }

        /// <summary>
        /// Metodo que hace la carga de los estatus existentes en la base de datos
        /// </summary>
        internal void LoadCurrentStatuses()
        {
            try
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.enterMessageInfo, "LoadCurrentStatuses"));

                _view.SessionCurrentStatuses = _controller.SelectAllStatuses();

                HttpContext.Current.Trace.Write(Messages.successfulTransactionMessage);
            }
            catch (ImperialException ie)
            {
                _view.SuccessfulTransaction = false;
                _view.TransactionMessage = ie.ImperialMessage;
            }
            catch (Exception e)
            {
                _view.SuccessfulTransaction = false;
                _view.TransactionMessage = Messages.failureTransactionMessage;
                _logger.WriteEventViewerLog("LoadCurrentStatuses", e.Message, true);
            }
            finally
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.exitMessageInfo, "LoadCurrentStatuses"));
            }
        }

        /// <summary>
        /// Metodo que realiza la insercion de un nuevo estatus
        /// </summary>
        internal void InsertNewStatus()
        {
            StatusEntity newStatus = null;
            try
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.enterMessageInfo, "InsertNewStatus"));

                if (!_controller.StatusAlreadyRegistered(_view.StatusNameText, _view.SessionCurrentStatuses))
                {
                    newStatus = _controller.CreateNewStatus(_view.StatusNameText);
                    if (_controller.InsertNewStatus(newStatus))
                    {
                        _view.SuccessfulTransaction = true;
                        _view.TransactionMessage = Messages.successfulTransactionMessage;
                    }
                }
                else
                {
                    _view.SuccessfulTransaction = false;
                    _view.TransactionMessage = Messages.duplicatedStatusMessage;
                }

                HttpContext.Current.Trace.Write(Messages.successfulTransactionMessage);
            }
            catch (ImperialException ie)
            {
                _view.SuccessfulTransaction = false;
                _view.TransactionMessage = ie.ImperialMessage;
            }
            catch (Exception e)
            {
                _view.SuccessfulTransaction = false;
                _view.TransactionMessage = Messages.failureTransactionMessage;
                _logger.WriteEventViewerLog("InsertNewStatus", e.Message, true);
            }
            finally
            {
                HttpContext.Current.Trace.Write(string.Format(Messages.exitMessageInfo, "InsertNewStatus"));
            }
        }
    }
}